package ownTemplates;

import javax.swing.*;
import java.awt.*;

/**
 * 〈一句话功能简述>
 * 〈〉
 *
 * @Author metype
 * @Create 2021/6/1
 * @Since 1.0.0
 */
public class AlgoFrame extends JFrame {

    /**  宽 */
    private Integer canvasWidth;
    /** 高 */
    private Integer canvasHeight;

    public AlgoFrame(String title, int canvasWidth, int canvasHeight) {
        super(title);
        this.canvasHeight = canvasHeight;
        this.canvasWidth = canvasWidth;

        AlgoCanvas algoCanvas = new AlgoCanvas();
        setContentPane(algoCanvas);
        pack();
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public Integer getCanvasWidth() {
        return canvasWidth;
    }

    public Integer getCanvasHeight() {
        return canvasHeight;
    }

    private Circle[] circles;
    public void render(Circle[] circles) {
        this.circles = circles;
        repaint();
    }

    /** 创建画布  */
    private class AlgoCanvas extends JPanel {

        public AlgoCanvas() {
            super(true);
        }

        /** Graphics：2-3 10  */
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            // 抗锯齿
            RenderingHints hints = new RenderingHints(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.addRenderingHints(hints);
            AlgoVisHelper.setStrokeWidth(g2d, 1);
            for (Circle circle : circles) {
                AlgoVisHelper.setColor(g2d, new Color(getRandomColor(), getRandomColor(), getRandomColor()));
                if (!circle.isFilled)
                    AlgoVisHelper.strokeCircle(g2d, circle.x, circle.y, circle.getR());
                else
                    AlgoVisHelper.fillCircle(g2d, circle.x, circle.y, circle.getR());

            }
        }

        /** 返回画布的大小  */
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(canvasWidth, canvasHeight);
        }

        private int getRandomColor() {
            return (int) (Math.random() * 254) + 1;
        }
    }

}